//
// Created by 董一龙 on 2022/10/13.
//

#ifndef FQUEUE_FQUEUE_H
#define FQUEUE_FQUEUE_H

#include <cstdint>
#include <string>
#include <memory>
#include "segment_queue/segment_queue.h"

namespace fqueue {
    class fqueue {
    public:
        explicit fqueue(std::string const& directory_name);

        // 透明的二进制流存储，分割协议自己定
        int32_t dequeue(void* output_buffer, size_t output_buffer_size);

        int32_t enqueue(void const* input_buffer, size_t input_buffer_size);

        int32_t dequeue(std::string& output_buffer);

        int32_t enqueue(std::string_view const& input_buffer);

    private:
        std::string m_base_directory_name;
        std::shared_ptr<segment_queue> m_pHeadPartQueue;
        std::shared_ptr<segment_queue> m_pTailPartQueue;

    private:
        std::shared_ptr<segment_queue> head();
        std::shared_ptr<segment_queue> tail();

        static segment_queue::index_t first_index(std::string const& directory_name);
        static segment_queue::index_t last_index(std::string const& directory_name);
        using index_range_t = std::tuple<segment_queue::index_t, segment_queue::index_t>;
        static index_range_t index_range(std::string const& directory_name);
    };
} // fqueue

#endif //FQUEUE_FQUEUE_H
